package cn.csq.lock;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ThreadMainTest {


    public static void main(String[] args) throws Exception {

        System.out.println(Thread.currentThread().getName() + " start , status " + Thread.currentThread().getState());

        A a = new A();
        a.setName("A");
        a.start();
        Thread.sleep(100);


        System.out.println(Thread.currentThread().getName() + " end , status " + Thread.currentThread().getState());

        // 异步任务
        FutureTask<String> futureTask = new FutureTask<>(new C());
        Thread thread1 = new Thread(futureTask);
        thread1.start();
        String futureTaskResult = futureTask.get();
        System.out.println("futureTaskResult = " + futureTaskResult);

        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        Thread.sleep(100000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }.start();
            // jps 定位到进程的id
            // jstack 堆栈的意思，查询进程中线程状态
        }

        B b = new B();
        Thread thread = new Thread(b);
        thread.setName("B");
        thread.start();


        // 线程暂停 停止标记不是直接停止线程
        thread.interrupt();
        thread.stop();
        thread.resume();
        thread.yield();
        thread.setDaemon(true);

    }

}


class A extends Thread {

    @Override
    public void run() {
        super.run();
        System.out.println(Thread.currentThread() + " running");
    }
}

class B implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread() + " running");
    }
}

class C implements Callable<String> {

    @Override
    public String call() throws Exception {
        return Thread.currentThread().getName() + "running" + " return call" + " result";
    }

}